function _toConsumableArray(arr) {
  return (
    _arrayWithoutHoles(arr) ||
    _iterableToArray(arr) ||
    _unsupportedIterableToArray(arr) ||
    _nonIterableSpread()
  );
}
function _nonIterableSpread() {
  throw new TypeError(
    'Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
  );
}
function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === 'string') return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === 'Object' && o.constructor) n = o.constructor.name;
  if (n === 'Map' || n === 'Set') return Array.from(o);
  if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
    return _arrayLikeToArray(o, minLen);
}
function _iterableToArray(iter) {
  if (
    (typeof Symbol !== 'undefined' && iter[Symbol.iterator] != null) ||
    iter['@@iterator'] != null
  )
    return Array.from(iter);
}
function _arrayWithoutHoles(arr) {
  if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  return arr2;
}
import { useTree } from '@szzj/hooks';
import { useMemo } from 'react';
import { matchPath } from 'react-router-dom';
/**
 * react-router-dom 没有导出 useMatches，手动实现一个
 * @param param0
 * @returns
 */
function useMatches(_ref) {
  var _ref$pathname = _ref.pathname,
    pathname = _ref$pathname === void 0 ? '/' : _ref$pathname,
    _ref$base = _ref.base,
    base = _ref$base === void 0 ? '/' : _ref$base,
    routes = _ref.routes;
  var _useTree = useTree(routes, {
      uniqueKey: 'path',
      childrenKey: 'routes',
    }),
    findNode = _useTree.findNode,
    toArray = _useTree.toArray,
    findPosterityNodesByKey = _useTree.findPosterityNodesByKey,
    findAncestorNodes = _useTree.findAncestorNodes;

  /** 基础路由节点 */
  var baseRoute = useMemo(
    function () {
      return findNode(base);
    },
    [findNode, base],
  );

  /** 基础路径下的路由节点，平铺数组形式 */
  var baseChildRoutes = useMemo(
    function () {
      return base && baseRoute ? findPosterityNodesByKey(base) : toArray(routes);
    },
    [findPosterityNodesByKey, toArray, baseRoute, base, routes],
  );

  /** 匹配当前访问路径的叶子路由节点 */
  var matchedRoute = useMemo(
    function () {
      var result = baseChildRoutes.find(function (route) {
        var _route$routes;
        return (
          !(
            (_route$routes = route.routes) !== null &&
            _route$routes !== void 0 &&
            _route$routes.length
          ) &&
          !!matchPath(
            {
              path: route.path,
            },
            pathname,
          ) &&
          route.path.split('/').length === pathname.split('/').length
        );
      });
      return result;
    },
    [baseChildRoutes, pathname],
  );

  /** 路由节点路径 */
  var matches = useMemo(
    function () {
      return matchedRoute !== null && matchedRoute !== void 0 && matchedRoute.path
        ? [baseRoute]
            .concat(
              _toConsumableArray(
                findAncestorNodes(matchedRoute.path)
                  .filter(function (route) {
                    return route.path.length > base.length;
                  })
                  .reverse(),
              ),
              [matchedRoute],
            )
            .filter(function (it) {
              return !!it;
            })
            .filter(function (it) {
              return it === baseRoute || !!it.title;
            })
        : [];
    },
    [baseRoute, findAncestorNodes, matchedRoute, base],
  );
  return matches;
}
export default useMatches;
